// componebts/popup/popup.js
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    name:{
      type:String,
      data:null,
    },
    //设置层是否展示修改手机号
    isshow: {
      type: Number,
      data: null,
    },

  },

  /**
   * 组件的初始数据
   */
  data: {
    checkname:'获取手机号',
    phone:'', //手机号
    phonecode:'', //手机验证码
    checkcount: 60,
    disabled: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //关闭层
      close:function(){
        this.setData({
          isshow:1
        })
      },
    //获取填写的手机号
    getphone: function (e) {
      this.setData({
        phone: e.detail.value
      })
    },
    getcode: function (e) {
      this.setData({
        phonecode: e.detail.value
      })
    },

    //获取验证码
    getchecknumber: function (e) {
      //手机号验证
      if (this.data.phone == "") {
        wx.showToast({
          title: '填写手机号',
          icon: 'none',
          duration: 2000
        });
        return false;
      }

      var that = this;
      const data = {
        wiki: 'phone',
        phone: that.data.phone
      }
      http.request({
        url: 'mini/v1/login/send',
        data: data,
        methods: 'post',
        success: function (res) {
          wx.showToast({
            title: '已发送，注意查收',
            icon: 'success',
            mask: true,
            duration: 2000
          });

          //验证码倒计时开始
          var countDown = setInterval(function () {
            if (that.data.checkcount <= 0) {
              that.setData({
                checkcount: 60,
                checkname: '获取验证码',
                disabled: false
              })
              clearInterval(countDown)
              return
            }
            that.data.checkcount--
            that.setData({
              checkcount: that.data.checkcount,
              disabled: true,
              checkname: that.data.checkcount < 10 ? `${that.data.checkcount}s` : `${that.data.checkcount}s`
            })
          }, 1000);
          //验证码倒计时结束
        }
      })
    },
    submit() {
      if (this.data.phone == "") {
        wx.showToast({
          title: '填写手机号',
          icon: 'none',
          duration: 2000
        });

        return false;
      }

      if (this.data.phonecode == "") {
        wx.showToast({
          title: '填写短信验证码',
          icon: 'none',
          duration: 2000
        });

        return false;
      }

      //登录
      var that = this;
      const data = { phone: that.data.phone, code: that.data.phonecode };
      http.request({
        url: 'mini/v1/personal/bind_phone',
        methods:'post',
        data: data,
        methods: 'post',
        success: function (res) {
          that.setData({
            isshow: 1
          })
          that.triggerEvent('showifevent', { showif: false,phone:that.data.phone});
        }
      })
    }
  }
})
